package com.pingan.project.autosendsms;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class ShareUtil {
    private SharedPreferences share;
    private static ShareUtil instance;
    private static final String SHARE_NAME = "AutoSMS";

    private ShareUtil(Context context) {
        share = context.getApplicationContext().getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
    }

    public static ShareUtil getInstance(Context context) {
        if (instance == null) {
            instance = new ShareUtil(context);
        }
        return instance;
    }

    public void commit(String key, String value) {
        share.edit().putString(key, value).commit();
    }

    public void commit(String key, int value) {
        share.edit().putInt(key, value).commit();
    }

    /////////////////////////////////////////////
    public void saveSMS(List<SmSBean> mDataList) {
        String str = new Gson().toJson(mDataList, new TypeToken<List<SmSBean>>() {
        }.getType());
        commit("KEY_SMS", str);
    }

    public ArrayList<SmSBean> getSMS() {
        String prefStr = share.getString("KEY_SMS", null);
        if (!TextUtils.isEmpty(prefStr)) {
            return new Gson().fromJson(prefStr, new TypeToken<List<SmSBean>>() {
            }.getType());
        } else {
            return new ArrayList<>();
        }
    }

    /*=========================*/
    public void saveSMSType(int smsType) {
        commit("KEY_SMS_TYPE", smsType);
    }

    public int getSMSType() {
        return share.getInt("KEY_SMS_TYPE", 0);
    }

}
